package com.shxt.framework.rbac.weeklycontent.service.impl;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.shxt.framework.rbac.user.model.User;
import com.shxt.framework.rbac.weeklycontent.model.WeeklyContent;
import jxl.Workbook;
import jxl.write.*;
import org.springframework.util.CollectionUtils;

/**
 * excel文件操作
 * @date 2019-12-05
 * @author stevenjob
 */
public class ExcelWriteService {



    public static void createExcel(OutputStream os, List<WeeklyContent> list) throws WriteException,IOException{
        //创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        //创建新的一页
        WritableSheet sheet = workbook.createSheet("First Sheet",0);
        //创建要显示的内容,创建一个单元格，第一个参数为列坐标，第二个参数为行坐标，第三个参数为内容
        sheet.addCell(new Label(0,0,"用户名"));
        sheet.addCell(new Label(1,0,"题目"));
        sheet.addCell(new Label(2,0,"内容"));
        sheet.addCell(new Label(3,0,"计划开始时间"));
        sheet.addCell(new Label(4,0,"计划结束时间"));
        sheet.addCell(new Label(5,0,"实际开始时间"));
        sheet.addCell(new Label(6,0,"实际结束时间"));

        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        for (int row = 1; list.size() > row - 1; row++) {
            WeeklyContent obj = list.get(row - 1);
            sheet.addCell(new Label(0,row,obj.getUser().getAccount()));
            sheet.addCell(new Label(1,row,obj.getTitle()));
            sheet.addCell(new Label(2,row,obj.getContent()));
            if (obj.getPlan_start() != null) {
                sheet.addCell(new DateTime(3, row, obj.getPlan_start()));
            }
            if (obj.getPlan_end() != null) {
                sheet.addCell(new DateTime(4, row, obj.getPlan_end()));
            }
            if (obj.getReal_start() != null) {
                sheet.addCell(new DateTime(5, row, obj.getReal_start()));
            }
            if (obj.getReal_end() != null) {
                sheet.addCell(new DateTime(6, row, obj.getReal_end()));
            }

        }

        //把创建的内容写入到输出流中，并关闭输出流
        workbook.write();
        workbook.close();
        os.close();
    }




    public static void main(String[] args) throws IOException, WriteException {
        FileOutputStream fos = new FileOutputStream("a.xls");
        WeeklyContent a = new WeeklyContent();
        a.setUser(new User());
        a.setContent("666666");
        a.setReal_end(new Date());
        new ExcelWriteService().createExcel(fos, Arrays.asList(a));
    }
    
}